<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Main interface to C code and data</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ffi.examples-complete.html">« A Complete PHP/FFI/preloading Example</a></li>
      <li style="float: right;"><a href="ffi.addr.html">FFI::addr »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.ffi.html">FFI</a></li>
    <li>Main interface to C code and data</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.ffi" class="reference">

 <h1 class="title">Main interface to C code and data</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 7 &gt;= 7.4.0, PHP 8)</p>


  <div class="section" id="ffi.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    Objects of this class are created by the factory methods <span class="methodname"><a href="ffi.cdef.html" class="methodname">FFI::cdef()</a></span>,
    <span class="methodname"><a href="ffi.load.html" class="methodname">FFI::load()</a></span> or <span class="methodname"><a href="ffi.scope.html" class="methodname">FFI::scope()</a></span>. Defined C variables
    are made available as properties of the FFI instance, and defined C functions are made available
    as methods of the FFI instance. Declared C types can be used to create new C data structures
    using <span class="methodname"><a href="ffi.new.html" class="methodname">FFI::new()</a></span> and <span class="methodname"><a href="ffi.type.html" class="methodname">FFI::type()</a></span>.
   </p>
   <p class="para">
    FFI definition parsing and shared library loading may take significant time. It is not useful
    to do it on each HTTP request in a Web environment. However, it is possible to preload FFI definitions
    and libraries at PHP startup, and to instantiate FFI objects when necessary. Header files
    may be extended with special <code class="literal">FFI_SCOPE</code> defines (e.g. <code class="code">#define FFI_SCOPE &quot;foo&quot;”&quot;</code>;
    the default scope is &quot;C&quot;) and then loaded by <span class="methodname"><a href="ffi.load.html" class="methodname">FFI::load()</a></span> during preloading.
    This leads to the creation of a persistent binding, that will be available to all the following
    requests through <span class="methodname"><a href="ffi.scope.html" class="methodname">FFI::scope()</a></span>.
    Refer to the <a href="ffi.examples-complete.html" class="link">complete PHP/FFI/preloading example</a>
    for details.
   </p>
   <p class="para">
    It is possible to preload more than one C header file into the same scope.
   </p>
  </div>


  <div class="section" id="ffi.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">final</span>
      <span class="modifier">class</span> <strong class="classname">FFI</strong>
     </span>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.addr.html" class="methodname">addr</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span> <code class="parameter reference">&$ptr</code></span>): <span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.alignof.html" class="methodname">alignof</a></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span>|<span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span></span> <code class="parameter reference">&$ptr</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.arraytype.html" class="methodname">arrayType</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$dimensions</code></span>): <span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.cast.html" class="methodname">cast</a></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span>|<span class="type">string</span></span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span>|<span class="type">int</span>|<span class="type">float</span>|<span class="type">bool</span>|<span class="type">null</span></span> <code class="parameter reference">&$ptr</code></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ffi.cast.html" class="methodname">cast</a></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span>|<span class="type">string</span></span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span>|<span class="type">int</span>|<span class="type">float</span>|<span class="type">bool</span>|<span class="type">null</span></span> <code class="parameter reference">&$ptr</code></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.cdef.html" class="methodname">cdef</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$code</code><span class="initializer"> = &quot;&quot;</span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$lib</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.ffi.html" class="type FFI">FFI</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.free.html" class="methodname">free</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span> <code class="parameter reference">&$ptr</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.isnull.html" class="methodname">isNull</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span> <code class="parameter reference">&$ptr</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.load.html" class="methodname">load</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.ffi.html" class="type FFI">FFI</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.memcmp.html" class="methodname">memcmp</a></span>(<span class="methodparam"><span class="type"><span class="type">string</span>|<span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span></span> <code class="parameter reference">&$ptr1</code></span>, <span class="methodparam"><span class="type"><span class="type">string</span>|<span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span></span> <code class="parameter reference">&$ptr2</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.memcpy.html" class="methodname">memcpy</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span> <code class="parameter reference">&$to</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span>|<span class="type">string</span></span> <code class="parameter reference">&$from</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.memset.html" class="methodname">memset</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span> <code class="parameter reference">&$ptr</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.new.html" class="methodname">new</a></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span>|<span class="type">string</span></span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$owned</code><span class="initializer"> = <strong><code>true</code></strong></span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$persistent</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ffi.new.html" class="methodname">new</a></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span>|<span class="type">string</span></span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$owned</code><span class="initializer"> = <strong><code>true</code></strong></span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$persistent</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.scope.html" class="methodname">scope</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>): <span class="type"><a href="class.ffi.html" class="type FFI">FFI</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.sizeof.html" class="methodname">sizeof</a></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span>|<span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span></span> <code class="parameter reference">&$ptr</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.string.html" class="methodname">string</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span> <code class="parameter reference">&$ptr</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$size</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.type.html" class="methodname">type</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$type</code></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ffi.type.html" class="methodname">type</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$type</code></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="ffi.typeof.html" class="methodname">typeof</a></span>(<span class="methodparam"><span class="type"><a href="class.ffi-cdata.html" class="type FFI\CData">FFI\CData</a></span> <code class="parameter reference">&$ptr</code></span>): <span class="type"><a href="class.ffi-ctype.html" class="type FFI\CType">FFI\CType</a></span></div>

   }</div>


  </div>

 </div>

 


































































































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="ffi.addr.html">FFI::addr</a> — Creates an unmanaged pointer to C data</li><li><a href="ffi.alignof.html">FFI::alignof</a> — Gets the alignment</li><li><a href="ffi.arraytype.html">FFI::arrayType</a> — Dynamically constructs a new C array type</li><li><a href="ffi.cast.html">FFI::cast</a> — Performs a C type cast</li><li><a href="ffi.cdef.html">FFI::cdef</a> — Creates a new FFI object</li><li><a href="ffi.free.html">FFI::free</a> — Releases an unmanaged data structure</li><li><a href="ffi.isnull.html">FFI::isNull</a> — Checks whether a FFI\CData is a null pointer</li><li><a href="ffi.load.html">FFI::load</a> — Loads C declarations from a C header file</li><li><a href="ffi.memcmp.html">FFI::memcmp</a> — Compares memory areas</li><li><a href="ffi.memcpy.html">FFI::memcpy</a> — Copies one memory area to another</li><li><a href="ffi.memset.html">FFI::memset</a> — Fills a memory area</li><li><a href="ffi.new.html">FFI::new</a> — Creates a C data structure</li><li><a href="ffi.scope.html">FFI::scope</a> — Instantiates an FFI object with C declarations parsed during preloading</li><li><a href="ffi.sizeof.html">FFI::sizeof</a> — Gets the size of C data or types</li><li><a href="ffi.string.html">FFI::string</a> — Creates a PHP string from a memory area</li><li><a href="ffi.type.html">FFI::type</a> — Creates an FFI\CType object from a C declaration</li><li><a href="ffi.typeof.html">FFI::typeof</a> — Gets the FFI\CType of FFI\CData</li></ul>
</div>
</div></div></body></html>